package com.example.apiproject.controller;

import com.example.apiproject.entity.Activities;
import com.example.apiproject.entity.form.AddActivityForm;
import com.example.apiproject.service.ActivitiesService;
import com.example.apiproject.utils.AppPageUtils;
import com.example.apiproject.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * (Activities)表控制层
 *
 * @author makejava
 * @since 2023-09-23 13:40:56
 */
@Api(tags = "活动接口")
@RestController
@RequestMapping("activities")
public class ActivitiesController {
    /**
     * 服务对象
     */
    @Resource
    private ActivitiesService activitiesService;




    /**
     * 查询最近进行的活动
     * @param request
     * @return
     */
    @GetMapping("/recent")
    public R queryRecent(HttpServletRequest request){

        return R.ok().put("result",activitiesService.queryRecent(request));
    }

    /**
     * 根据用户id查询用户参与的活动
     * @param request 用户id
     * @return 活动集合
     */
    @GetMapping("/user")
    public R queryByUserId(HttpServletRequest request,Activities activities,@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                           @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize){
        return R.ok().put("result",activitiesService.queryByUserId(request,activities,pageNum,pageSize));

    }


    @GetMapping("/querybyclass")
    public R queryByClass(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                          @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize, @RequestParam("type") Integer type){
        AppPageUtils page = activitiesService.queryPageListBytype(pageNum,pageSize,type);

        return R.ok().put("result", page);
    }

    /**
     * 条件分页查询
     * @param activities
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping
    public R queryByPage(Activities activities, @RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                                                        @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize) {

            return R.ok().put("result",activitiesService.queryByPage(activities,pageNum,pageSize));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询")
    public ResponseEntity<Activities> queryById(@PathVariable("id") String id) {
        return ResponseEntity.ok(this.activitiesService.queryById(id));
    }

    /**
     * 新增数据
     *
     *
     * @return 新增结果
     */
    @PostMapping("/add")
    public R add(@RequestBody AddActivityForm addActivityForm , HttpServletRequest request) {
        int id = activitiesService.insert(addActivityForm,request);
        if(id==0){
              return R.error("活动发布失败");
        }
        return R.ok().put("result",id);
    }

    /**
     * 编辑数据
     *
     * @param activities 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Integer> edit(Activities activities) {
        return ResponseEntity.ok(this.activitiesService.update(activities));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.activitiesService.deleteById(id));
    }

}

